package com.lanchetech.user.service.impl;


import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.common.enums.*;
import com.lanchetech.dao.GiveIdentityRecordMapper;
import com.lanchetech.dao.IdentityMapper;
import com.lanchetech.dao.UserMapper;
import com.lanchetech.entity.GiveIdentityRecord;
import com.lanchetech.entity.Identity;
import com.lanchetech.entity.User;
import com.lanchetech.service.LevelService;
import com.lanchetech.user.service.IdentityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service

public class IdentityServiceImpl implements IdentityService {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserMapper userMapper;

    @Autowired
    IdentityMapper identityMapper;

    @Autowired
    LevelService levelService;

    @Autowired
    GiveIdentityRecordMapper giveIdentityRecordMapper;

    @Override
    public BaseResp createPartner(String phone) {
        // 判断此用户是否有权限
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (!user.getStatus().equals(UserStatusEnum.NORMAL.getStatus())) {
            return new BaseResp(ResultCode.USER_BAN);
        }

        Identity identity = identityMapper.findOneByUserId(user.getId());

        if(identity == null || identity.getPartnerAccount() <= 0){
            return new BaseResp(ResultCode.PARTNER_ACCOUNT_NOT_ENOUGH);
        }

        if (identity == null || identity.getLevel() < UserLevelEnum.SHAREHOLDER_L1.getLevel()) {
            return new BaseResp(ResultCode.NOT_SHAREHOLDER);
        }

        User partnerUser = userMapper.findOneByUsernameAndStatus(phone, UserStatusEnum.NORMAL.getStatus());

        if (partnerUser == null) {
            return new BaseResp(ResultCode.ACCT_NOT_EXIST);
        }

        if (!partnerUser.getStatus().equals(UserStatusEnum.NORMAL.getStatus())) {
            return new BaseResp(ResultCode.USER_BAN);
        }

        // 赠送合伙人
        levelService.addOnePartner(partnerUser, IsBuyerEnum.GIFT.getType());

        // 减去赠送名额
        identity.setPartnerAccount(identity.getPartnerAccount() - 1);
        identityMapper.updateByPrimaryKeySelective(identity);

        //添加赠送合伙人记录
        giveIdentityRecordMapper.insert(GiveIdentityRecord.builder()
                .fromUserId(user.getId())
                .toUserId(partnerUser.getId())
                .type(IdentityTypeEnum.PARTNER.getType())
                .createdAt(new Date())
                .build());

        return new BaseResp();
    }

    @Override
    public BaseResp createMember(String phone) {
        // 判断此用户是否有权限
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (!user.getStatus().equals(UserStatusEnum.NORMAL.getStatus())) {
            return new BaseResp(ResultCode.USER_BAN);
        }

        Identity identity = identityMapper.findOneByUserId(user.getId());

        if (identity == null || identity.getMemberAccount() <= 0) {
            return new BaseResp(ResultCode.MEMBER_ACCOUNT_NOT_ENOUGH);
        }

        User memberUser = userMapper.findOneByUsernameAndStatus(phone, UserStatusEnum.NORMAL.getStatus());

        if (memberUser == null) {
            return new BaseResp(ResultCode.ACCT_NOT_EXIST);
        }

        if (!memberUser.getStatus().equals(UserStatusEnum.NORMAL.getStatus())) {
            return new BaseResp(ResultCode.USER_BAN);
        }

        // 赠送会员
        levelService.addOneMember(memberUser, IsBuyerEnum.GIFT.getType(), MemberStateEnum.SUPER.getType());

        // 减去赠送名额
        identity.setMemberAccount(identity.getMemberAccount() - 1);
        identityMapper.updateByPrimaryKeySelective(identity);

        //添加赠送合伙人记录
        giveIdentityRecordMapper.insert(GiveIdentityRecord.builder()
                .fromUserId(user.getId())
                .toUserId(memberUser.getId())
                .type(IdentityTypeEnum.MEMBER.getType())
                .createdAt(new Date())
                .build());

        return new BaseResp();
    }
}